<template>
  <div>
      <van-nav-bar
  title="极客学院-移动端"
/>
<van-form @submit="onSubmit">
  <van-field
  required
    v-model="mobile"
    name="手机号"
    label="手机号"
    placeholder="手机号"
    :rules="[{ required: true, message: '正确手机号',pattern:/^1[3-9]\d{9}$/ }]"
  />
  <van-field
  required
    v-model="code"
    type="password"
    name="验证码"
    label="验证码"
    placeholder="验证码"
    :rules="[{ required: true, message: '请填写6位验证码',pattern:/^\d{6}$/ }]"
  />
  <div style="margin: 16px;">
    <van-button
    round
    block
    type="info"
    native-type="submit"
    :disabled="isShow"
    :loading="isShow"
    loading-text="加载中"
    >提交</van-button>
  </div>
</van-form>
  </div>
</template>

<script>
import { setStorage } from '@/utils/storage'
import { Toast } from 'vant'
import { loginAPI } from '@/api'
import { setToken } from '@/utils/token'
export default {
  data () {
    return {
      mobile: '15176919953',
      code: '246810',
      isShow: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.isShow = true
      try {
        const res = await loginAPI(this.mobile, this.code)
        // console.log('登录', res)
        setToken(res.data.data.token)
        setStorage('refresh_token', res.data.data.refresh_token)
        this.isShow = false
        // Toast.success('登录成功')
        this.$notify('登录成功')
        console.log('submit', values)
        this.$router.replace({
          path: this.$route.query.pa || '/layout'
        })
      } catch (err) {
        console.error(err)
        this.isShow = false
        Toast.fail('登录失败')
      }
    }
  }
}
</script>

<style>

</style>
